﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.IBLL.BaseSystem.AreaManagement;
using BF.Model;
using WebDataControls;
using Base.Core.UnityFactory;
using System.Configuration;

namespace BF.Web.pages.BaseSystem.AreaManagement
{
    public partial class AreaEdit : BasePage
    {
        private readonly Isys_AreaBLL iBLL = IOCFactory.GetIOCResolve<Isys_AreaBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["sID"]))
                {
                    SysAreaTree1.SelectedAreaCode = Request["sID"].ToString();
                    SysAreaTree1.Enabled = false;
                    
                   // AreaControl.SetSysAreaValue(Request["sID"]);

                }
                else
                {
                    dic_Organization model = GetSessionCurrentUserInfo.dic_Organization;
                    if (model == null)
                    {
                        //SysAreaTree1.RootAreaCode = GetSysParam("AreaCode");
                        //SysAreaTree1.SelectedAreaCode = GetSysParam("AreaCode");
                        //  AreaControl.SetSysAreaValue(GetSysParam("AreaCode"));
                    }
                    else
                    {
                        SysAreaTree1.RootAreaCode = model.sAreaCode;
                        SysAreaTree1.SelectedAreaCode = model.sAreaCode;
                        //  AreaControl.SetSysAreaValue(model.sDivisionCode);
                        // DropDownList ddl = new DropDownList();
                    }
                }
                PageControl();
                //查看页面
                if (!String.IsNullOrEmpty(Request.QueryString["State"]) && (Request.QueryString["State"] == "View"))
                {
                    btnAdd.Visible = false;
                    btnSave.Visible = false;
                    EnabledControls(this.Form);
                    SysAreaTree1.Visible = false;
                    SysAreaTreeName.Text = SysAreaTree1.SelectedAreaName;
                }
                else
                {
                    SysAreaTreeName.Visible = false;
                }
            }
        }
        //private void EnabledControls(System.Web.UI.Control control)
        //{
        //    for (int i = 0; i < control.Controls.Count; i++)
        //    {
        //        System.Web.UI.Control tmp = control.Controls[i];

        //        if (tmp is TextBox)
        //        {
        //            (tmp as TextBox).ReadOnly = true;
        //        }
        //        if (tmp is DropDownList)
        //        {
        //            DropDownList ddl = (tmp as DropDownList);
        //            ddl.Parent.Controls.AddAt(i, new TextBox()
        //            {
        //                Text = (ddl.SelectedItem.Text == "--请选择--") ? "" : ddl.SelectedItem.Text,
        //                ReadOnly = true
        //            });
        //            ddl.Visible = false;
        //            i++;
        //        }
        //        if (tmp is CheckBox)
        //        {
        //        }
        //        else if (tmp.Controls.Count > 0)
        //            EnabledControls(tmp);

        //    }


        //}

        /// <summary>
        /// 加载页面控制的方法
        /// </summary>
        private void PageControl()
        {
            if (!string.IsNullOrEmpty(Request["sID"]))
            {
                sys_Area model = model = iBLL.GetModelByID(Request.QueryString["sID"]);
                WebUIHelper.SetControlsValue(model, "sys_Area", this.Form);
                if (model.sID.Length == 9)
                {
                    this.sID.Text = model.sID.Substring(6, 3);
                }
                else if (model.sID.Length == 12)
                {
                    this.sID.Text = model.sID.Substring(9, 3);
                }
                else
                {
                    this.sID.Text = model.sID;
                }
               // AreaControl.SetHideControl(5);
               // AreaControl.SetEnabledControl(5);
                this.sID.Enabled = false;
                this.sName.Enabled = false;
            }
            else
            {
              //  AreaControl.SetHideControl(5);
            }
        }

        #region 页面事件方法
        ///// <summary>
        ///// 新增
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void btnAdd_Click(object sender, EventArgs e)
        //{
        //    //ClientScript.RegisterStartupScript(this.GetType(), "", "function a(){ window.location.href='" + Request.Url.AbsolutePath + "';selfWin.title='新增行政区划')");

        //    Response.Redirect(Request.Url.AbsolutePath);
        //}

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            sys_Area model = GetModel();
            if (string.IsNullOrEmpty(Request["sID"]))
            {
                if (iBLL.GetModelByID(model.sID) != null)
                {
                    Messages("编码已经存在!");
                    return;
                }
                if (iBLL.Add(model))
                {
                    Messages("新增成功!", "function(){winer.RefurbishParent();UpatePage('" + model.sID.ToString() + "');}", this);

                }
                else
                {
                    Messages("新增失败!");
                }
                return;
            }
            else
            {
                if (iBLL.Update())
                {
                    Messages("修改成功!", "function(){winer.RefurbishParent();}", this);
                }
                else
                {
                    Messages("修改失败!");
                }
                return;
            }
        }
        #endregion

        #region 实体赋值
        private sys_Area GetModel()
        {
            sys_Area model = null;
            if (!string.IsNullOrEmpty(Request["sID"]))
            {
                model = iBLL.GetModelByID(Request.QueryString["sID"]);//主键 地区sID
                model.sName = this.sName.Text.Trim();//地区名称sName
                //有效标识	iFlag	1:有效  0:无效
            }
            else
            {
                model.sSuperID = SysAreaTree1.SelectedAreaCode;
                model.iLevel = iBLL.GetLevelByAreaCode(SysAreaTree1.SelectedAreaCode) + 1;//添加的是当前编码级别的子级，需要+1
                model.iLevelName = GetiLevelName(model.iLevel);//等级名称
                model.iFlag = 1;//默认有效是1
                if (model.iLevel > 3)
                {
                    model.sID = SysAreaTree1.SelectedAreaCode + this.sID.Text.Trim();
                }
                else
                {
                    model.sID = this.sID.Text.Trim();
                }
            }
            model = (sys_Area)WebUIHelper.GetControlsModel(model, "sys_Area", this.Form);
            //model.sOperator//操作员	sOperator
            return model;
        }
        #endregion

        /// <summary>
        /// 根据行政级别等级取得级别等级名称
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private string GetiLevelName(int level)
        {
            switch (level)
            {
                case 0:
                    return "国家级";
                case 1:
                    return "省级";
                case 2:
                    return "市级";
                case 3:
                    return "县、区级";
                case 4:
                    return "镇级";
                case 5:
                    return "村级";
                case 6:
                    return "组级";
                default:
                    return "不在级别范围内!";

            }
        }
        //主键 地区sID
        //地区名称sName
        //父IDsSuperID
        //级别iLevel
        //级别名称iLevelName
        //下设乡镇数	iCountry
        //下设行政村数	iVillage
        //下设组数	iGroup	
        //辖区人口总数	iSumPop	
        //农业人口总数	iAGRPop		
        //五保户人口数	iWBHPop	
        //贫困户人口数	iPoorPop	
        //暂住人口数	iLodgePop	
        //新农合参合户数	iJoinDoor	
        //新农合参合人数	iJoinPop		
        //邮政编码	sPostCode		
        //新农合对照编码	sXNHCode	
        //新农合对照名称	sXNHName		
        //有效标识	iFlag	1:有效  0:无效
        //数据入库时间	dOperator	
        //操作员	sOperator





    }
}